package view.backend.product;

import exceptionManager.ExceptionManager;
import java.awt.Font;
import java.awt.event.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import model.ProductFamily;
import view.generaldesign.DesignLayout;

/**
 *
 * @author Sonny Sandberg
 */
public class AdminNewProduct 
{
    private JPanel 
            adminNewProductPanel,
            productFamilySearchPanel;
    private AdminProduct adminProduct;
    private ProductFamily[] prdFmly;
    private JLabel
            lblName,
            lblPurchasePrice,
            lblSalesPrice,
            lblSize,
            lblProductFamily;
    private JTextField
            tFieldName,
            tFieldPurchase,
            tFieldSale,
            tFieldSize,
            tFieldProductFamily;
    private JCheckBox
            cBoxHasDecoration,
            cBoxServiceProduct;

    public AdminNewProduct(AdminProduct view)
    {
        adminProduct = view;
        
        adminNewProductPanel = new JPanel(null);
        adminNewProductPanel.setBounds(0, 0, 440, 255);
        adminNewProductPanel.setBorder(BorderFactory.createTitledBorder(DesignLayout.T_FIELD_RED, "Ny Vare", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, DesignLayout.STANDARD));
        adminNewProductPanel.setOpaque(false);
        
        buildNewProductPanel();
        
        adminNewProductPanel.setVisible(false);
    }
    
    private void buildNewProductPanel()
    {
        productFamilySearchPanel = new JPanel(null);
        productFamilySearchPanel.setBounds(315, 16, 120, 234);
        productFamilySearchPanel.setBorder(DesignLayout.T_FIELD_RED);
        productFamilySearchPanel.setVisible(true);
        productFamilySearchPanel.addComponentListener(new ComponentAdapter() 
        {
            @Override
            public void componentShown(ComponentEvent e)
            {
                doLookUp();
            }
        });
        adminNewProductPanel.add(productFamilySearchPanel);
        
        lblName = new JLabel("Navn");
        lblName.setBounds(10, 25, 300, 18);
        lblName.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldName = new JTextField();
        tFieldName.setBounds(10, 45, 300, 25);
        tFieldName.setBorder(DesignLayout.T_FIELD_RED);
        tFieldName.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldName.setFont(DesignLayout.STANDARD);
        tFieldName.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                adminProduct.getProductModel().setName(tFieldName.getText());
                adminProduct.getAdminNewProductOverview().updateLabelsFromModel();
            }
        });
        
        lblPurchasePrice = new JLabel("Købspris");
        lblPurchasePrice.setBounds(10, 80, 145, 18);
        lblPurchasePrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblPurchasePrice.setHorizontalAlignment(SwingConstants.CENTER);
        
        tFieldPurchase = new JTextField();
        tFieldPurchase.setBounds(10, 100, 145, 25);
        tFieldPurchase.setBorder(DesignLayout.T_FIELD_RED);
        tFieldPurchase.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldPurchase.setFont(DesignLayout.STANDARD);
        tFieldPurchase.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldPurchase.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldPurchase.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldPurchase.select(0, 0);
            }
        });
        tFieldPurchase.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                try
                {
                    adminProduct.getProductModel().setPurchasePrice(Double.parseDouble(tFieldPurchase.getText()));
                    adminProduct.getAdminNewProductOverview().updateLabelsFromModel();
                } 
                catch (NumberFormatException evt)
                {
                    tFieldPurchase.setText(""+adminProduct.getProductModel().getPurchasePrice());
                    adminProduct.getAdminNewProductOverview().updateLabelsFromModel();
                }                               
            }
        });
        
        lblSalesPrice = new JLabel("Salgspris");
        lblSalesPrice.setBounds(165, 80, 145, 18);
        lblSalesPrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblSalesPrice.setHorizontalAlignment(SwingConstants.CENTER);
        
        tFieldSale = new JTextField();
        tFieldSale.setBounds(165, 100, 145, 25);
        tFieldSale.setBorder(DesignLayout.T_FIELD_RED);
        tFieldSale.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldSale.setFont(DesignLayout.STANDARD);
        tFieldSale.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldSale.addFocusListener(new FocusListener() 
        {
            @Override
            public void focusGained(FocusEvent e)
            {
                tFieldSale.selectAll();
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                tFieldSale.select(0, 0);
            }
        });
        tFieldSale.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                try
                {
                    adminProduct.getProductModel().setSalesPrice(Double.parseDouble(tFieldSale.getText()));
                    adminProduct.getAdminNewProductOverview().updateLabelsFromModel();  
                } 
                catch (NumberFormatException evt)
                {
                    tFieldSale.setText(""+adminProduct.getProductModel().getSalesPrice());
                    adminProduct.getAdminNewProductOverview().updateLabelsFromModel();
                }              
            }
        });
        
        lblSize = new JLabel("Størrelse");
        lblSize.setBounds(10, 135, 80, 18);
        lblSize.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblSize.setHorizontalAlignment(SwingConstants.CENTER);
        
        tFieldSize = new JTextField();
        tFieldSize.setBounds(10, 155, 80, 25);
        tFieldSize.setBorder(DesignLayout.T_FIELD_RED);
        tFieldSize.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldSize.setFont(DesignLayout.STANDARD);
        tFieldSize.setHorizontalAlignment(SwingConstants.CENTER);
        tFieldSize.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                adminProduct.getProductModel().setSize(tFieldSize.getText());
                adminProduct.getAdminNewProductOverview().updateLabelsFromModel(); 
            }
        });
        
        lblProductFamily = new JLabel("Varegruppe");
        lblProductFamily.setBounds(100, 135, 200, 18);
        lblProductFamily.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        
        tFieldProductFamily = new JTextField();
        tFieldProductFamily.setBounds(100, 155, 210, 25);
        tFieldProductFamily.setBorder(DesignLayout.T_FIELD_RED);
        tFieldProductFamily.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        tFieldProductFamily.setFont(DesignLayout.STANDARD);
        tFieldProductFamily.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyReleased(KeyEvent e)
            {
                doLookUp();
            }
        });
        
        tFieldProductFamily.addFocusListener(new FocusListener() {

            @Override
            public void focusGained(FocusEvent e)
            {
                productFamilySearchPanel.setVisible(true);
            }

            @Override
            public void focusLost(FocusEvent e)
            {
                productFamilySearchPanel.setVisible(false);
            }
        });
        
        cBoxHasDecoration = new JCheckBox("Har Dekoration");
        cBoxHasDecoration.setBounds(10, 190, 300, 20);
        cBoxHasDecoration.setOpaque(false);
        cBoxHasDecoration.addItemListener(new ItemListener() 
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                adminProduct.getProductModel().setHasDecoration(cBoxHasDecoration.isSelected());
                adminProduct.getAdminNewProductOverview().updateLabelsFromModel(); 
            }
        });
        
        cBoxServiceProduct = new JCheckBox("Serviceprodukt (multisalg)");
        cBoxServiceProduct.setBounds(10, 215, 300, 20);
        cBoxServiceProduct.setOpaque(false);
        cBoxServiceProduct.addItemListener(new ItemListener() 
        {
            @Override
            public void itemStateChanged(ItemEvent e)
            {
                adminProduct.getProductModel().setRemainAfterSale(cBoxServiceProduct.isSelected());
                adminProduct.getAdminNewProductOverview().updateLabelsFromModel();                 
            }
        });
        
        adminNewProductPanel.add(lblName);
        adminNewProductPanel.add(tFieldName);
        adminNewProductPanel.add(lblPurchasePrice);
        adminNewProductPanel.add(tFieldPurchase);
        adminNewProductPanel.add(lblSalesPrice);
        adminNewProductPanel.add(tFieldSale);
        adminNewProductPanel.add(lblSize);
        adminNewProductPanel.add(tFieldSize);
        adminNewProductPanel.add(lblProductFamily);
        adminNewProductPanel.add(tFieldProductFamily);
        adminNewProductPanel.add(cBoxHasDecoration);
        adminNewProductPanel.add(cBoxServiceProduct);
    }
    
    private void doLookUp()
    {
        productFamilySearchPanel.removeAll();
        productFamilySearchPanel.setBorder(DesignLayout.T_FIELD_RED);
        try
        {
            prdFmly = model.handlers.ProductFamilyHandler.getProductFamilyArrayBySearch(tFieldProductFamily.getText());
            if (prdFmly.length > 1)
            {
                for (int i = 0; i < prdFmly.length; i++)
                {
                    productFamilySearchPanel.setVisible(true);
                    AdminProductFamilySearchLabel lbl = new AdminProductFamilySearchLabel(prdFmly[i], adminProduct);
                    lbl.setBounds(1, 1+(i*20), 118, 20);
                    if (i == prdFmly.length-1)
                    {
                        lbl.setBorder(null);
                    }
                    productFamilySearchPanel.add(lbl);
                }
            }
            else if (prdFmly.length == 1)
            {
                tFieldProductFamily.setText(prdFmly[0].getName());
                adminProduct.getProductModel().setProductFamily(prdFmly[0]);
                adminProduct.getAdminNewProductOverview().updateLabelsFromModel();
            }
            else if (prdFmly.length < 1)
            {
                productFamilySearchPanel.setVisible(false);
            }

        } 
        catch (SQLException ex)
        {
            ExceptionManager.handleSQLException(ex);
        } 
        catch (ClassNotFoundException ex)
        {
            ExceptionManager.handleClassNotFoundException(ex);
        }
        
        productFamilySearchPanel.revalidate();
        productFamilySearchPanel.repaint();
    }
    
    public void setProductFamilyField(String str)
    {
        tFieldProductFamily.setText(str);
    }
    
    public void setProductFamilySearchPanelVisibility(boolean visibilty)
    {
        productFamilySearchPanel.setVisible(true);
    }
    
    public void setFieldsFromModel()
    {
        tFieldName.setText(adminProduct.getProductModel().getName());        
        tFieldPurchase.setText(""+adminProduct.getProductModel().getPurchasePrice());
        tFieldSale.setText(""+adminProduct.getProductModel().getSalesPrice());
        tFieldSize.setText(adminProduct.getProductModel().getSize());
        tFieldProductFamily.setText(adminProduct.getProductModel().getProductFamily().getName());
        cBoxHasDecoration.setSelected(adminProduct.getProductModel().isHasDecoration());
        cBoxServiceProduct.setSelected(adminProduct.getProductModel().isRemainAfterSale());
    }
    
    public void reset()
    {
        prdFmly = null;
        productFamilySearchPanel.removeAll();
        productFamilySearchPanel.revalidate();
        productFamilySearchPanel.repaint();
    }

    public JPanel getPanel()
    {
        return adminNewProductPanel;
    }
}